package cz.muni.fi.pb162.project.geometry;

/**
 * Instance of this class represents a 2D point with X and Y coordinates
 * 
 * @author Martin Pasko 
 * @version 2012-10-01 project01
 */

public class Vertex2D implements Comparable<Vertex2D>
{

    private double x;
    private double y;
    public static final double EPSILON = 0.0001;

/**
 * Constructor of Vertex2D class
 * 
 * @param xx x-coordinate of a point
 * @param yy y-coordinate of a point
*/

    public Vertex2D(double xx, double yy){
        x=xx; 
        y=yy;
    }
          
    
    public double getX(){
        return x;
    }
    
    public double getY(){
        return y;
    }
    
 /**
 * Method that returns distance between two points (calling instance and parameter instance).
 * 
 * @param second point to which I want to compute distance
 * 
 * @return -1.0 if parameter is null, otherwise returns distance between calling instance and parameter instance
 */
        
    public double distance(Vertex2D vert){
        if(vert == null) {
            return -1.0; 
        }
        return Math.sqrt(Math.pow(vert.getX()-this.getX(), 2)+Math.pow(vert.getY()-this.getY(), 2));
    }
    
    /**
     * Compares two vertices according to their coordinates
     * @param obj vertex to be compared
     * @return true if it has the same coordinates as the calling vertex, false all other cases
     */
    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Vertex2D)) {
            return false;
        }
        if(this == obj) {
            return true;
        }
        return Math.abs(this.getX() - ((Vertex2D)obj).getX()) < EPSILON && Math.abs(this.getY() - ((Vertex2D)obj).getY()) < EPSILON;
    }
    
    /**
     * Computes hash code for the vertex
     * @return hash code of the vertex. Different vertices may have the same hash code, if one of them has the X coordinate zero.
     */
    @Override
    public int hashCode() {
        return (int) (getX()*10000 + getY());
    }
    
    @Override
    public int compareTo(Vertex2D vert) {
        if(Math.abs(this.getX() - vert.getX()) < EPSILON) {
            if(Math.abs(this.getY() - vert.getY()) < EPSILON) {
                return 0;
            }
            if((this.getY() - vert.getY()) < 0) {
                return -1;
            }
            return 1;
        }
        if((this.getX() - vert.getX()) < 0) {
            return -1;
        }
        return 1;
    }
    
 /**
 * Vertex2D toString method
 * 
 * @return x and y coordinates of a Vertex2D instance
 */

    @Override
    public String toString(){
        return "["+this.x+", "+this.y+"]";
    }
        
        
 
}
